class Member:
    """会员类，包含会员等级和对应的折扣率"""

    def __init__(self, level='普通会员'):
        """
        初始化会员
        :param level: 会员等级，可选 '普通会员', '银卡会员', '金卡会员', '钻石会员'
        """
        self.level = level
        self.discount_rates = {
            '普通会员': 1.0,  # 无折扣
            '银卡会员': 0.9,  # 9折
            '金卡会员': 0.85,  # 85折
            '钻石会员': 0.8  # 8折
        }

    @property
    def discount_rate(self):
        """获取当前会员等级的折扣率"""
        return self.discount_rates.get(self.level, 1.0)

    def upgrade(self, new_level):
        """升级会员等级"""
        if new_level in self.discount_rates:
            self.level = new_level
            return True
        return False

    def __str__(self):
        return f"{self.level}({self.discount_rate * 10}折)"


class Product:
    """商品类"""

    def __init__(self, id, name, price):
        """
        初始化商品
        :param id: 商品ID
        :param name: 商品名称
        :param price: 商品价格
        """
        self.id = id
        self.name = name
        self.price = price

    def __str__(self):
        return f"{self.id}-{self.name} ￥{self.price:.2f}"


class CartItem:
    """购物车商品项"""

    def __init__(self, product, quantity, member=None):
        """
        初始化购物车商品项
        :param product: 商品对象
        :param quantity: 商品数量
        :param member: 会员对象，可选
        """
        self.product = product
        self.quantity = quantity
        self.member = member if member else Member()  # 默认普通会员

    @property
    def subtotal(self):
        """计算小计（未打折）"""
        return self.product.price * self.quantity

    @property
    def discounted_subtotal(self):
        """计算打折后的小计"""
        return self.subtotal * self.member.discount_rate

    def __str__(self):
        discount_info = ""
        if self.member.discount_rate < 1.0:
            original = self.subtotal
            discounted = self.discounted_subtotal
            discount_info = f" (会员价: ￥{discounted:.2f}, 节省 ￥{original - discounted:.2f})"

        return (f"{self.product} × {self.quantity} = ￥{self.subtotal:.2f}"
                f"{discount_info}")


class ShoppingCart:
    """购物车类"""

    def __init__(self):
        """初始化购物车"""
        self.items = []

    def add_item(self, product, quantity=1, member=None):
        """添加商品到购物车"""
        for item in self.items:
            if item.product.id == product.id:
                item.quantity += quantity
                break
        else:
            self.items.append(CartItem(product, quantity, member))

    def remove_item(self, product_id):
        """从购物车移除商品"""
        self.items = [item for item in self.items if item.product.id != product_id]

    @property
    def total(self):
        """计算总价（未打折）"""
        return sum(item.subtotal for item in self.items)

    @property
    def discounted_total(self):
        """计算打折后的总价"""
        return sum(item.discounted_subtotal for item in self.items)

    def apply_discount(self):
        """应用折扣，返回折扣后的总价"""
        return self.discounted_total

    def show(self):
        """显示购物车内容及折扣信息"""
        print("购物车内容:")
        print("-" * 50)
        for item in self.items:
            print(item)
        print("-" * 50)

        if any(item.member.discount_rate < 1.0 for item in self.items):
            print(f"商品总价: ￥{self.total:.2f}")
            print(f"会员折扣后总价: ￥{self.discounted_total:.2f}")
            print(f"您节省了: ￥{self.total - self.discounted_total:.2f}")
        else:
            print(f"商品总价: ￥{self.total:.2f} (无会员折扣)")

        print("=" * 50)


# 示例使用
if __name__ == "__main__":
    # 创建一些商品
    p1 = Product(1, "苹果", 5.5)
    p2 = Product(2, "香蕉", 3.2)
    p3 = Product(3, "牛奶", 8.0)
    p4 = Product(4, "鸡蛋", 12.5)

    # 创建不同等级的会员
    regular_member = Member('普通会员')
    silver_member = Member('银卡会员')
    gold_member = Member('金卡会员')
    diamond_member = Member('钻石会员')

    # 创建购物车
    cart = ShoppingCart()

    # 添加商品到购物车，有些商品使用会员折扣
    cart.add_item(p1, 3)  # 普通会员
    cart.add_item(p2, 2, silver_member)  # 银卡会员
    cart.add_item(p3, 1, gold_member)  # 金卡会员
    cart.add_item(p4, 2, diamond_member)  # 钻石会员

    # 显示购物车内容
    cart.show()

    # 输出会员信息
    print("\n会员信息:")
    print(f"普通会员: {regular_member}")
    print(f"银卡会员: {silver_member}")
    print(f"金卡会员: {gold_member}")
    print(f"钻石会员: {diamond_member}")